# Make file for generate firmware package.
#  1. place some needed files to images dir (eg. zImage uboot dtb ...)
#  2. run 'make bootimage' and 'make package'

SOC			?= imx6q
LDO			?=
# BOARDS		?= d7
# BOARDS		?= d7a
# BOARDS		?= d10
# BOARDS		?= d20
BOARDS		?= d4

# Android data partition size in MB
ADATA_SIZE	?= 3073

ROOTEXEC	:= sudo
IMAGEDIR	:= ../images
AND_IMAGES	:= ../android_images
ROOTFSDIR	:= ../zy_rootfs/fb-gui-rfs
APPSFSDIR	:= ../zy_rootfs/apps-fs
CACHEDIR	:= ../zy_rootfs/cache
DEVICDIR	:= ../zy_rootfs/devic
TOOLSDIR	:= ./bin
VERIFY_CFG	:= ./verifys.txt
VERSION_CFG	:= ./version.txt

DATE_STRING	:= `date +%Y%m%d`
VER_STRING	:= `cat $(VERSION_CFG)`
VD_STRING	:= $(VER_STRING)-$(DATE_STRING)

COMANDLINE	:= comandline
ROOTIMAGE	:= $(IMAGEDIR)/rootfs.img
APPSIMAGE	:= $(IMAGEDIR)/appsfs.img
MBR_IMAGE	:= $(IMAGEDIR)/mbr.img
CACHE_IMAGE	:= $(IMAGEDIR)/cache.img
DEVIC_IMAGE	:= $(IMAGEDIR)/devic.img

XLOGOIMAGE	:= ../zy_rootfs/loga.bmp

KERNEL		:= $(IMAGEDIR)/zImage
MODULE		:= $(IMAGEDIR)/modules.tar.bz2
DEVTREE		:= $(IMAGEDIR)/$(SOC)-$$BD$(LDO).dtb

UPD_FILE	:= UPDATE-$$BD$(LDO)-$(VD_STRING).bin

MKMBXIMG	:= $(TOOLSDIR)/mkmbximg
MKMBRIMG	:= $(TOOLSDIR)/mkmbrimg
MKBOOTIMG	:= $(TOOLSDIR)/mkbootimg
MKROOTIMG	:= $(TOOLSDIR)/make_ext4fs
COMANDSTR	?= $(shell cat $(COMANDLINE))

# for host build toolchain
CC		:= gcc
CXX		:= g++
CPP		:= gcc -E
AS		:= as
AR		:= ar
LD		:= ld
NM		:= nm
STRIP	:= strip
RANLIB	:= ranlib
OBJDUMP	:= objdump
OBJCOPY	:= objcopy

CFLAGS	:=
LDFLAGS	:=

TOOLS_BUILD_SHARED ?=

export CC CXX CPP AS AR LD NM STRIP RANLIB OBJDUMP OBJCOPY
export CFLAGS LDFLAGS TOOLS_BUILD_SHARED

## get a verify value from verify file
# $(1): board name
# $(2): verify values file
define get_verify
	`grep $(1) $(2) -w | cut -d':' -f 2`
endef

# check linux app dir exist
# $(1): board name
define check_app_dir
	if [ -d $(APPSFSDIR)-$(1) ] ; then \
		echo $(APPSFSDIR)-$(1) ; \
	else \
		echo $(APPSFSDIR) ; \
	fi
endef

all : tools bootimage rootfs package

tools : ext4 mincrypt mkbootimg pcre selinux sparse mkmbximg mkmbrimg
	-rm -rvf $(TOOLSDIR)/*.a

mincrypt :
	$(MAKE) -j2 -C tools/libmincrypt
	$(MAKE) -C tools/libmincrypt install

ext4 : selinux sparse
	$(MAKE) -j2 -C tools/ext4_utils
	$(MAKE) -C tools/ext4_utils install

mkbootimg : mincrypt
	$(MAKE) -j2 -C tools/mkbootimg
	$(MAKE) -C tools/mkbootimg install

pcre :
	$(MAKE) -j2 -C tools/pcre
	$(MAKE) -C tools/pcre install

selinux : pcre
	$(MAKE) -j2 -C tools/libselinux
	$(MAKE) -C tools/libselinux install

sparse :
	$(MAKE) -C tools/libsparse
	$(MAKE) -C tools/libsparse install

mkmbximg :
	$(MAKE) -j2 -C tools/mkmbximg
	$(MAKE) -C tools/mkmbximg install

mkmbrimg :
	$(MAKE) -j2 -C tools/mkmbrimg
	$(MAKE) -C tools/mkmbrimg install

clean :
	@echo "CLEAN tool utils..."
	@rm -rf $(TOOLSDIR)
	$(MAKE) -C tools/libmincrypt $@
	$(MAKE) -C tools/ext4_utils $@
	$(MAKE) -C tools/mkbootimg $@
	$(MAKE) -C tools/pcre $@
	$(MAKE) -C tools/libselinux $@
	$(MAKE) -C tools/libsparse $@
	$(MAKE) -C tools/mkmbximg $@
	$(MAKE) -C tools/mkmbrimg $@

mbrimage :
	$(MKMBRIMG) -s $(ADATA_SIZE) $(MBR_IMAGE)
	@echo "Generate MBR image done : $(MBR_IMAGE)"

cacheimage :
	@mkdir -p $(CACHEDIR)
	@mkdir -p $(DEVICDIR)
	@rm -rf $(CACHEDIR)/*
	@rm -rf $(DEVICDIR)/*
	$(ROOTEXEC) $(MKROOTIMG) -s -l 500M $(CACHE_IMAGE) $(CACHEDIR)
	$(ROOTEXEC) $(MKROOTIMG) -s -l 7M $(DEVIC_IMAGE) $(DEVICDIR)
	@echo "Generate android cache image done : $(CACHE_IMAGE) + $(DEVIC_IMAGE)"

mbr_package : $(MKMBXIMG) mbrimage cacheimage
	@for BD in $(BOARDS) ; do \
		echo "Generate mbr_package for board : $$BD$(LDO)" ; \
		$(MKMBXIMG) --verify $(call get_verify,$$BD,$(VERIFY_CFG)) \
				--mbr $(MBR_IMAGE) \
				--cache $(CACHE_IMAGE) \
				--devic $(DEVIC_IMAGE) \
				--output $(IMAGEDIR)/MBR-$(UPD_FILE) ; \
	done

uboot_package : $(MKMBXIMG)
	@for BD in $(BOARDS) ; do \
		echo "Generate uboot package for board : $$BD$(LDO)" ; \
		[ -e $(IMAGEDIR)/u-boot-$$BD$(LDO).imx ] && \
			$(MKMBXIMG) --verify $(call get_verify,$$BD,$(VERIFY_CFG)) \
				--uboot $(IMAGEDIR)/u-boot-$$BD$(LDO).imx \
				--output $(IMAGEDIR)/UBOOT-$(UPD_FILE) ; \
	done

bootimage : $(MKBOOTIMG)
	@echo Commandline : $(COMANDSTR)
	@for BD in $(BOARDS) ; do \
		echo "Generate bootimage for board : $$BD$(LDO)" ; \
		[ -e $(DEVTREE) ] && \
			$(MKBOOTIMG) --kernel $(KERNEL) \
				--second $(DEVTREE) \
				--module $(MODULE) --cmdline "$(COMANDSTR)" \
				--output $(IMAGEDIR)/boot-$$BD$(LDO).img; \
	done

rootfs : $(MKROOTIMG)
	@echo $(VD_STRING) > $(ROOTFSDIR)/home/version.log
	$(ROOTEXEC) $(MKROOTIMG) -s -l 250M $(ROOTIMAGE) $(ROOTFSDIR)
	@echo "Generate root filesystem image done : $(ROOTIMAGE)"

apps : $(MKROOTIMG)
	@for BD in $(BOARDS) ; do \
		$(ROOTEXEC) $(MKROOTIMG) -s -l 80M $(APPSIMAGE:.img=-$$BD.img) `$(call check_app_dir,$$BD)` ; \
		echo "Generate apps filesystem image done : $(APPSIMAGE:.img=-$$BD.img)" ; \
	done

apps_update : $(MKROOTIMG)
	@for BD in $(BOARDS) ; do \
		echo "Generate app package for board : $$BD" ; \
		$(MKMBXIMG) --verify $(call get_verify,$$BD,$(VERIFY_CFG)) \
			--lapps $(APPSIMAGE:.img=-$$BD.img) \
			--output $(IMAGEDIR)/APP-$(UPD_FILE) ; \
	done

kernel_update : bootimage
	@for BD in $(BOARDS) ; do \
		echo "Generate kernel package for board : $$BD$(LDO)" ; \
		[ -e $(IMAGEDIR)/boot-$$BD$(LDO).img ] && \
			$(MKMBXIMG) --verify $(call get_verify,$$BD,$(VERIFY_CFG)) \
			--lboot $(IMAGEDIR)/boot-$$BD$(LDO).img \
			--output $(IMAGEDIR)/KERNEL-$(UPD_FILE) ; \
	done

kernel_app_update : bootimage
	@for BD in $(BOARDS) ; do \
		echo "Generate kernel and app package for board : $$BD$(LDO)" ; \
		[ -e $(IMAGEDIR)/boot-$$BD$(LDO).img ] && \
			$(MKMBXIMG) --verify $(call get_verify,$$BD,$(VERIFY_CFG)) \
				--lapps $(APPSIMAGE:.img=-$$BD.img) --lboot $(IMAGEDIR)/boot-$$BD$(LDO).img \
				--output $(IMAGEDIR)/KERNEL-APP-$(UPD_FILE) ; \
	done

linux_package package : $(MKMBXIMG) $(MKROOTIMG) bootimage apps rootfs
	@for BD in $(BOARDS) ; do \
		echo "Generate linux package for board : $$BD$(LDO)" ; \
		[ -e $(IMAGEDIR)/boot-$$BD$(LDO).img ] && \
			$(MKMBXIMG) --verify $(call get_verify,$$BD,$(VERIFY_CFG)) \
				--xlogo $(XLOGOIMAGE) \
				--lboot $(IMAGEDIR)/boot-$$BD$(LDO).img \
				--lroot $(ROOTIMAGE) \
				--lapps $(APPSIMAGE:.img=-$$BD.img) \
				--output $(IMAGEDIR)/LLLL-$(UPD_FILE) ; \
	done

android_package : $(MKMBXIMG)
	@for BD in $(BOARDS) ; do \
		echo "Generate android package for board : $$BD$(LDO)" ; \
		[ -e $(AND_IMAGES)/boot-$$BD$(LDO).img ] && \
			$(MKMBXIMG) --verify $(call get_verify,$$BD,$(VERIFY_CFG)) \
				--xlogo $(XLOGOIMAGE) \
				--aboot $(AND_IMAGES)/boot-$$BD$(LDO).img \
				--rboot $(AND_IMAGES)/recovery-$$BD$(LDO).img \
				--aroot $(AND_IMAGES)/system.img \
				--adata $(AND_IMAGES)/userdata.img \
				--output $(IMAGEDIR)/AAAA-$(UPD_FILE) ; \
	done

all_package : $(MKMBXIMG) mbrimage cacheimage
	@for BD in $(BOARDS) ; do \
		echo "Generate full package for board : $$BD$(LDO)" ; \
		[ -e $(IMAGEDIR)/boot-$$BD$(LDO).img ] && \
			$(MKMBXIMG) --verify $(call get_verify,$$BD,$(VERIFY_CFG)) \
				--mbr $(MBR_IMAGE) \
				--cache $(CACHE_IMAGE) \
				--devic $(DEVIC_IMAGE) \
				--xlogo $(XLOGOIMAGE) \
				--lboot $(IMAGEDIR)/boot-$$BD$(LDO).img \
				--lroot $(ROOTIMAGE) \
				--lapps $(APPSIMAGE:.img=-$$BD.img) \
				--aboot $(AND_IMAGES)/boot-$$BD$(LDO).img \
				--rboot $(AND_IMAGES)/recovery-$$BD$(LDO).img \
				--aroot $(AND_IMAGES)/system.img \
				--adata $(AND_IMAGES)/userdata.img \
				--output $(IMAGEDIR)/LLAA-$(UPD_FILE) ; \
	done



.PHONY: all tools bootimage rootfs apps clean package android_package linux_package \
		mbrimage cacheimage mbr_package
